<?php
/**
 * 类别
 * User: lyl
 * Date: 2019-04-29
 */

namespace Biz\module\Classes\WikiClean;

use Doraemon\model\Category as mCategory;

class Category
{
    private static $filter_value = [];

    /**
     * 执行统一操作
     **/
    public static function run($params, &$data){
        self::_filterData();
        self::_add($params, $data);
    }

    private static function _filterData(){
        self::$filter_value=[];
        //self::$filter_value = mCategory::select(['id','cate_name'],['status'=>mCategory::STATUS_USING]);
    }

    /**
     * 插入表
     **/
    private static function _add($params, &$data){
        $source_data = json_decode($params['source_data'], true);

        //商品对应类别
        if(isset($data['goods']) && !empty($data['goods'])){
            $data['goods']['one_category_id'] = 0;
            $data['goods']['two_category_id'] = 0;
            $data['goods']['last_category_id'] = 0;
            $data['goods']['category'] = '';


            if(isset($source_data['params'])  && !empty($source_data['params'])) {
                foreach ($source_data['params'] as $_sk=>&$_sv) {
                    if(preg_match('{(分类)}', $_sv['label'])){
                        //$data['category'] = self::_filter($_sv['value']);
                        if(!empty(self::_filter($_sv['value']))) {
                            $data['goods']['one_category_id'] = self::_filter($_sv['value'])['one_id'];
                            $data['goods']['two_category_id'] = self::_filter($_sv['value'])['two_id'];
                            $data['goods']['last_category_id'] = self::_filter($_sv['value'])['id'];
                            $data['goods']['category'] = self::_filter($_sv['value'])['cate_name'];
                        }
                        break;
                    }
                }
            }
            if(!isset($data['goods']['category']) && !empty(self::_filter($data['goods']['name']))){
                $data['goods']['one_category_id'] = self::_filter($data['goods']['name'])['one_id'];
                $data['goods']['two_category_id'] = self::_filter($data['goods']['name'])['two_id'];
                $data['goods']['last_category_id'] = self::_filter($data['goods']['name'])['id'];
                $data['goods']['category'] = self::_filter($data['goods']['name'])['cate_name'];
            }
        }
    }

    private static function _filterTree($param, $name = ''){
        $result = '';
        foreach($param as $_k=>&$_v){
            if(!empty($name) && preg_match('{('.$_v['cate_name'].')}', $name)){
                $tree = mCategory::getStrTreeById($_v['id'],0);
                $tree = explode('/',$tree);
                $_v['one_id'] = $tree[0];
                $_v['two_id'] = $tree[1];
                $result = $_v;
                break;
            }
        }
        return $result;
    }

    private static function _filter($param){
        $data = self::_filterTree(self::$filter_value, $param);
        return $data;
    }

}